package com.knife.bbs.business;

import java.util.Date;


import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;
import com.knife.bbs.domain.WebMessage;

public class MessageUtil {

	public static boolean del(WebMessage wm, String username) {
		if (wm.getType().intValue() == WebMessage.SYSTEMSENDMSG) {
			return wm.del();
		}
		if (wm.getType().intValue() == WebMessage.NORMALMSG) {
			if (username.equals(wm.getFromname())) {
				if (wm.getStatus().intValue() == WebMessage.NORMAL) {
					wm.setStatus(new Integer(WebMessage.SENDDEL));
					return wm.update();
				} else if (wm.getStatus().intValue() == WebMessage.REVDEL) {
					return wm.del();
				}
			} else if (username.equals(wm.getToname())) {
				if (wm.getStatus().intValue() == WebMessage.NORMAL) {
					wm.setStatus(new Integer(WebMessage.REVDEL));
					return wm.update();
				} else if (wm.getStatus().intValue() == WebMessage.SENDDEL) {
					return wm.del();
				}
			}
		}
		return true;
	}

	public static boolean read(WebMessage wm) {
		if (wm.getIsread().intValue() == WebMessage.UNREAD) {
			wm.setIsread(new Integer(WebMessage.READ));
			return wm.update();
		}
		return true;
	}

	public static boolean isread(WebMessage wm) {
		if (wm.getIsread().intValue() == WebMessage.UNREAD) {
			return true;
		}
		return false;
	}

	public static IPageList queryUnRead(String scope, int pageSize, int pageNo) {
		DbPageList pList = new DbPageList(WebMessage.class, scope
				+ " and isread=" + WebMessage.UNREAD + " and status in ("
				+ WebMessage.NORMAL + " , " + WebMessage.SENDDEL
				+ ") and type in (" + WebMessage.NORMALMSG + " , "
				+ WebMessage.SYSTEMSENDMSG + ")", null);
		pList.doList(pageNo, pageSize);
		return pList;
	}

	public static IPageList querySend(String scope, int pageSize, int pageNo) {
		DbPageList pList = new DbPageList(WebMessage.class, scope
				+ " and type=" + WebMessage.NORMALMSG + " and status in ("
				+ WebMessage.NORMAL + " , " + WebMessage.REVDEL + " )", null);
		pList.doList(pageNo, pageSize);
		return pList;
	}

	public static IPageList queryRecv(String scope, int pageSize, int pageNo) {
		DbPageList pList = new DbPageList(WebMessage.class, scope
				+ " and status in (" + WebMessage.NORMAL + " , "
				+ WebMessage.SENDDEL + " ) and type in ("
				+ WebMessage.NORMALMSG + " , " + WebMessage.SYSTEMSENDMSG
				+ " )", null);
		pList.doList(pageNo, pageSize);
		return pList;
	}

	public static boolean sendSystemMessage(String type, String toname) {
		WebMessage wm = (WebMessage) BBSConfig.getInstance().getMessages().get(type.trim());
		wm.setCid(null);
		String message=wm.getMessage();
		message=message.replaceAll("#username",toname);
		wm.setMessage(message);
		wm.setType(new Integer(WebMessage.SYSTEMSENDMSG));
		wm.setStatus(new Integer(WebMessage.NORMAL));
		wm.setToname(toname);
		wm.setIsread(new Integer(WebMessage.UNREAD));
		wm.setSenddate(new Date());
		wm.setAck(new Integer(0));
		return wm.save();
	}
	
		
}
